package cn.bopomo.dealer.base.action.ajax;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.json.JSONObject;

import cn.bopomo.dealer.base.action.BaseAction;
import cn.bopomo.dealer.base.common.ServiceConfig;
import cn.bopomo.dealer.base.service.IOperatorService;
import cn.bopomo.dealer.base.util.ResultCode;
import cn.bopomo.dealer.base.vo.AuthorizeList;

public class LoadAuthorizeAjax extends BaseAction {

	public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
		//AJAX返回设置
		response.setContentType("text/html;charset=UTF-8");
		response.setHeader("Cache-control","no-cache");
		
		StringBuffer result = new StringBuffer();
		String op_result = ResultCode.OP_FAILED;;
		AuthorizeList authorize = null;
		try {
			//获取参数
			Integer aid = new Integer(request.getParameter("aid"));
			//获取Service
			IOperatorService os = (IOperatorService)this.findService(ServiceConfig.OPERATOR_SERVICE);
			//获取"权限"实例
			authorize = os.getAuthorize(aid);
			//
			if (authorize != null){
				//JSONArray  array = this.jsonProblems(module);
				//建立JSON实例
				JSONObject json = new JSONObject();
				//JSONSerializer js = new JSONSerializer();
				//js.registerDefaultSerializers();
				//js.toJSON(module);
				//写入JSON属性
				json.put("authorizeId", authorize.getAid());
				json.put("authorizeName", authorize.getAuthorizeName());
				json.put("authorizeAction", authorize.getAuthorizeAction());
				json.put("authorizeTag", authorize.getAuthorizeTag());
				json.put("addDate", authorize.getAddDate().toLocaleString());
				log.info(authorize.getAid() + ":" + authorize.getAuthorizeName() );
				//返回JSON对象
				response.getWriter().write(json.toString());
			}
		} catch (Exception e) {
			log.error(e);
			op_result = ResultCode.OP_FAILED;
			//组合返回信息
			result.append(op_result).append("||")
			.append(this.getCodeDescription(op_result,request.getSession().getServletContext().getResource(this.codeDescXMLPath).getPath()));
			log.info(result);
			//返回 
			response.getWriter().write(result.toString());
		}
		return null;
		
	}

}
